﻿using System;
using System.Collections;
using App_Code.Modules;
using Portal.Core;
using Portal.Core.Helper;
using Portal.Core.Web.UI;
using Portal.Data.Entity;
using Portal.Data.Webparts;

public partial class Modules_WebpartPage_Edit : WebpartPageModule
{
    public string CategoryName;
    protected int PageID;
    private WebpartPage _wpp;
    const string OtherPageUrl = "/admin_WebpartPage_WebpartPage_editpage.aspx?id={0}";
    private const string DetailPageName = "Trang chi tiết";
    private const string ListPageName = "Trang danh sách";
    protected override string Action
    {
        get { return "editpage"; }
    }

    protected WebLayout PageLayout
    {
        get
        {
            return (WebLayout)Page.LoadControl(_wpp.Layout().Path);
        }
    }
    private void GetEditObject()
    {
        var obj = EditObject;
        if (obj != null)
        {
            _wpp = (WebpartPage)obj;
            PageID = _wpp.ID;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        LoadEditForm();

        if (Apply.Visible)
        {
            LoadWebpage();
        }
    }

    private void LoadWebpage()
    {
        GetEditObject();
        var page = new WebpartPageControl { Info = _wpp, ViewMode = PageViewMode.Design };

        var icon = _wpp.Layout().Icon;
        if (!string.IsNullOrEmpty(icon))
            imgLayout.ImageUrl = CoreContext.absStaticFile + icon;
        else
            imgLayout.Visible = false;

        Category cat = Category.FindFirstByWebpartId(_wpp.ID);
        if (cat != null)
        {
            if (cat.DetailPage_ID == _wpp.ID)
            {
                btnGoOther.Text =ListPageName;
                hidOtherPageId.Value = cat.ListPage_ID.ToString();
                CategoryName = DetailPageName + " - " + cat.FullName;
            }
            else
            {
                btnGoOther.Text = DetailPageName;
                hidOtherPageId.Value = cat.DetailPage_ID.ToString();
                CategoryName = ListPageName + " - " + cat.FullName;
            }
        }

        WebPage.Controls.Add(page);

        if (!IsPostBack)
        {
            cboWebpartDef.DataSource = WebpartDefinition.ListAll();
            cboWebpartDef.DataBind();

            IList list = PageLayout.allZones();

            foreach (string zone in list)
            {
                Zones.Items.Add(zone);
            }
        }
    }

    protected new void Cancel_Click(object sender, EventArgs e)
    {
        Forward("cancel");
    }

    protected void Apply_Click(object sender, EventArgs e)
    {
        var page = (WebpartPage)GetObject();
        if (page == null)
        {
            return;
        }

        page.Update();
        page.ChangeWebpartsOrder(Request["order_store"]);
        Recache(page.ID);
        Forward("apply");
    }

    protected new void Save_Click(object sender, EventArgs e)
    {
        var page = (WebpartPage)GetObject();
        if (page == null)
        {
            return;
        }
        page.ChangeWebpartsOrder(Request["order_store"]);
        Recache(page.ID);
        Forward("save");
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        var page = (WebpartPage)GetObject();
        if (page == null)
        {
            return;
        }
        string zone = Zones.SelectedValue;
        int defId = Convert.ToInt32(cboWebpartDef.SelectedValue);

        var part = new Webpart
        {
            Zone = zone,
            Definition_ID = defId,
            Page_ID = page.ID,
            WebPartOrder = Webpart.GetNextOrder(page.ID)
        };
        part.Save();
        Recache(page.ID);
        Forward("add");
    }

    void Recache(int wppId)
    {
        Webpart.FindByWebpartPage(wppId, true);
    }

    protected void GoOther(object sender, EventArgs e)
    {
        Response.Redirect(string.Format(OtherPageUrl, hidOtherPageId.Value));
    }
}